<template>
	<view class="index">
		<u--form ref="uForm">
			<u-form-item prop="phoneNumber">
				<u--input v-model="phoneNumber" placeholder="请输入手机号" />
			</u-form-item>
			<u-form-item prop="password">
				<u--input type="password" v-model="password" placeholder="请输入新密码(6-20位英文或数字)" />
			</u-form-item>
			<u-form-item prop="smsCode">
				<view class="sms_box">
					<u--input customStyle="margin-top:0" v-model="smsCode" placeholder="请输入验证码" />
					<view class="sms_btn" @click="sendCode()">{{countdown?countdown+'秒':'发送验证码'}}</view>
				</view>
			</u-form-item>
		</u--form>
		<u-button class="btn" type="primary" @click="submit">确定</u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phoneNumber: '',
				password: "",
				smsCode: '',
				codeSend: false,
				countdown: 0,
				timer: null,
				refreshFlag: false
			}
		},
		onLoad() {

		},
		methods: {
			async sendCode() {
				this.countdown = 60;
				uni.showLoading({
					title: '正在获取验证码'
				})
				this.$api.login.sendSMS({
					phoneNumber: this.phoneNumber
				}).then(() => {
					// 这里此提示会被this.start()方法中的提示覆盖
					uni.$u.toast('验证码已发送');
				}).finally(() => {
					uni.hideLoading();
				})
				this.timer = setInterval(() => {
					this.countdown--
				}, 1000)
			},
			async submit(e) {
				if (!this.phoneNumber) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					})
				}
				if (!this.smsCode) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
				}
				if (!this.password) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
				}
				let reg = /^[0-9a-zA-Z]{6,20}$/
				if (!reg.test(this.password)) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确格式的密码'
					})
				}
				let res = await this.$api.login.forgetPassword({
					smsCode: this.smsCode,
					newPassword: this.password,
					phoneNumber: this.phoneNumber
				})
				if (res.code === 0) {
					uni.showToast({
						title: '修改密码成功',
						icon: 'none'
					})
					setTimeout(() => {
						uni.reLaunch({
							url: '/views/login/index'
						})
					}, 500)
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			}
		},
		watch: {
			countdown(e) {
				// console.log(e)
				if (e == 0) {
					clearInterval(this.timer)
				}
			}

		}
	}
</script>

<style lang="scss" scoped>
	.index {
		width: 100%;
		height: 100%;
		padding: 30rpx;
		background-color: white;
		box-sizing: border-box;
		// display: flex;
		// flex-direction: column;
	}

	::v-deep .u-form-item__body__right__message {
		margin-left: 0 !important;
	}

	.sms_box {
		width: 100%;
		display: flex;
		gap: 24rpx;
		align-items: center;
		// margin-top: 20rpx;
		border-radius: 8rpx;

		.sms_btn {
			width: 220rpx;
			height: 90rpx;
			background-color: #FAFAFA;
			text-align: center;
			line-height: 90rpx;
			font-size: 29rpx;
			color: #A0A1A3;
			border-radius: 8rpx;
		}
	}

	.btn {
		margin-top: 20rpx;
	}

	.text {
		font-size: 24rpx;
		margin-top: 20rpx;
		display: flex;
		align-items: center;
		gap: 20rpx;

		.check_box {
			width: 24rpx;
			height: 24rpx;
			border-radius: 4rpx;
			border: 1rpx solid rgba(0, 0, 0, 0.3);
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.blue_text {
			color: #3392FF;

		}
	}


	::v-deep .u-input {
		height: 90rpx !important;
		box-sizing: border-box;
		background-color: #FAFAFA;
		border: none;
		// flex: 0 !important;
		// margin-top: 20rpx;
	}
</style>